package com.neulogistics.contrib.procuredcenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.procuredcenter.api.application.command.CreateGoodsListCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateGoodsListCommand;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.GoodsList;
import com.neulogistics.contrib.procuredcenter.domain.repository.IGoodsListRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class UpdateGoodsListCommandHandler implements IRequestHandler<UpdateGoodsListCommand, BaseResponse> {
    @Resource
    IGoodsListRepository goodsListRepository;

    @Override
    public BaseResponse handle(UpdateGoodsListCommand command) throws Exception {
        GoodsList goodsList = goodsListRepository.findById(command.listId);

        if(goodsList == null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }

        goodsList.setListName(command.listName);
        goodsList.setListDescription(command.listDescription);

        goodsListRepository.save(goodsList);

        return ResultUtils.success(null);
    }
}
